﻿/**
 *    Copyright 2012 Yohan Courbe
 *
 *  Licensed under the Apache License, Version 2.0 (the "License") 
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *      
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Timers;
using CountryLookup;
using PacketDotNet;

namespace NetworkAnalyzer.Engines.Graphics.Radar
{
    public class GraphicsRadarSystem : Engine
    {
        public static readonly object Locker = new object();
        public static readonly List<RadarCountry> RadarCountries = new List<RadarCountry>();
        public static GraphicsRadarMode Mode = GraphicsRadarMode.TotalBandwidthRadarMode;

        private static readonly Lookup Cl = new Lookup(@"Dependencies/GeoIP.dat");
        private static Timer activityTimer = new Timer();

        public static bool Enabled = false;

        public GraphicsRadarSystem()
        {
            activityTimer.Interval = 1000;
            activityTimer.Elapsed += ActivityTimerOnElapsed;
            activityTimer.AutoReset = true;
            activityTimer.Enabled = true;
        }

        public override void AddPacket(Packet packet)
        {
            
        }

        private void ActivityTimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
        {
            lock(Locker)
            {
                foreach (var radarCountry in RadarCountries.Where(r => r.Shown = true && r.Ttl < 0))
                {
                    radarCountry.ResetActivity();
                }
            }
        }

        public override void Start()
        {
            Enabled = true;
            activityTimer.Start();
        }

        public override void Stop()
        {
            Enabled = false;
            activityTimer.Stop();
        }

        public override void AddIpTcpPacket(Packet packet, IpPacket ipPacket, TcpPacket tcpPacket)
        {
            if (!Enabled) return;
            
            Country country;
            ApplicativeProtocol applicativeProtocol;

            if (ipPacket.SourceAddress.IsLocalHost())
            {
                country = Cl.LookupCountry(ipPacket.DestinationAddress);
                //Console.WriteLine(ip.DestinationAddress);
                applicativeProtocol = Tools.GetApplicativeProtocol(tcpPacket.DestinationPort);
            }
            else
            {
                country = Cl.LookupCountry(ipPacket.SourceAddress);
                //Console.WriteLine(ip.SourceAddress);
                applicativeProtocol = Tools.GetApplicativeProtocol(tcpPacket.SourcePort);
                if (country.CountryCode.Equals("--"))
                    return;
            }

            lock (Locker)
            {
                foreach (var radarCountry in RadarCountries.Where(radarCountry => radarCountry.Country.Equals(country)))
                {
                    radarCountry.IncreaseActivity(packet.Bytes.Length);
                    return;
                }

                var newRadarCountry = new RadarCountry(country);
                newRadarCountry.IncreaseActivity(packet.Bytes.Length);
                RadarCountries.Add(newRadarCountry);
            }
        }

        public override void AddIpUdpPacket(Packet packet, IpPacket ipPacket, UdpPacket udpPacket)
        {
            if (!Enabled) return;

        }
    }

    public enum GraphicsRadarMode
    {
        TotalBandwidthRadarMode,
        UploadBandwidthRadarMode,
        DownloadBandwidthRadarMode
    }
}
